<template>
  <div>
    <el-cascader
      ref="area"
      size="small"
      :key="is_cascader"
      v-model="addressArr"
      :props="addressProps"
      :options="province"
      style="width: 100%"
      @change="handleCas"
      placeholder="请选择"
    >
      <template slot-scope="{ node, data }">
        <span> {{ getLevel(node.level) }}{{ data.label }}</span>
      </template>
    </el-cascader>
  </div>
</template>

<script>
let that = null;
import { getTechnology } from "@/api/technology";
let id = 0;
export default {
  computed: {
    getLevel() {
      return function (level) {
        let val;
        switch (level) {
          case 1:
            val = "零件号:";
            break;
          case 2:
            val = "工序号:";
            break;
          case 3:
            val = "版次号:";
            break;
          default:
            break;
        }
        return val;
      };
    },
  },
  props: {
    templateId: {
      type: [Number, String],
      default: "",
    },
  },
  data() {
    return {
      leafList: [],
      province: [], //这个数据是组件初始化created异步加载的数据，里面加空对象是为了el-cascader初始化没有数据报错 data=null这个原因
      addressArr: [], //绑定的数据
      is_cascader: Math.random(), //重新渲染标识key
      isShowInfo: true,
      addressInit: false, //标记组件第一级数据开始加载没有

      addressProps: {
        lazy: true, //开启异步加载
        lazyLoad(node, resolve) {
          if (!that.templateId) return;
          const { value, level, data } = node;
          let val = {};
          let key = "";
          //   第一级是零件号
          console.log("that.templateId :>> ", data);
          if (level == 0) {
            key = "partNumber";
            val = {
              templateId: that.templateId,
            };
          }
          // 工序号
          if (level == 1) {
            key = "processNumber";
            val = {
              partNumber: data.partNumber,
            };
          }

          // 版本
          if (level == 2) {
            key = "version";
            val = {
              processNumber: data.processNumber,
              partNumber: data.partNumber,
            };
          }
          var params = {
            page: 1,
            pageSize: 0,
            ...val,
          };
          getTechnology(params).then((res) => {
            // this.$emit("lazyLoad", data, level);
            if (level == 2) {
              that.leafList = res.data.results;
              resolve(
                res.data.results.map((item) => {
                  return {
                    ...item,
                    leaf: "leaf", //标识最后一级
                    label: item[key],
                    value: item["id"],
                  };
                })
              );
            } else {
              resolve(
                res.data.results.map((item) => {
                  return {
                    ...item,
                    label: item[key],
                    value: item[key],
                  };
                })
              );
            }
          });
        },
      },
    };
  },
  created() {
    that = this;
  },
  methods: {
    handleCas(val) {
      const id = val[val.length - 1];
      that.$emit(
        "lazyLoad",
        this.leafList.find((item) => item.id == id)
      );
    },
  },
};
</script>

<style lang="scss" scoped>
</style>